package FlowGram.Application.CommandHost;

import java.awt.*;  
import java.awt.event.*;  
import javax.swing.*;  

import FlowGram.Application.CommandHost.FinchActionListeners.BeakColorListener;
import FlowGram.Application.CommandHost.FinchActionListeners.ChirpListener;
import FlowGram.Application.CommandHost.FinchActionListeners.CommentListener;
import FlowGram.Application.CommandHost.FinchActionListeners.DropBeeperListener;
import FlowGram.Application.CommandHost.FinchActionListeners.FunctionDefListener;
import FlowGram.Application.CommandHost.FinchActionListeners.FunctionInvocationListener;
import FlowGram.Application.CommandHost.FinchActionListeners.IfListener;
import FlowGram.Application.CommandHost.FinchActionListeners.IncludeListener;
import FlowGram.Application.CommandHost.FinchActionListeners.PickBeeperListener;
import FlowGram.Application.CommandHost.FinchActionListeners.RepeatListener;
import FlowGram.Application.CommandHost.FinchActionListeners.WhileListener;
import FlowGram.Application.Parser.Utility.ClassFinder;
   
public class ExpandingPanels extends MouseAdapter  
{  
    ActionPanel[] aps;  
    JPanel[] panels;  
    
    CommandHost ch;
    
    public ExpandingPanels()  
    {  
        assembleActionPanels();  
        assemblePanels();  
    }
    
    public ExpandingPanels(CommandHost ch){
    	assembleActionPanels();  
    	assemblePanels();  
    	this.ch =ch;
    }
   
    public void mousePressed(MouseEvent e)  
    {  
        ActionPanel ap = (ActionPanel)e.getSource();  
        if(ap.target.contains(e.getPoint()))  
        {  
            ap.toggleSelection();  
            togglePanelVisibility(ap);  
        }  
    }  
   
    private void togglePanelVisibility(ActionPanel ap)  
    {  
        int index = getPanelIndex(ap);  
        if(panels[index].isShowing()){
            panels[index].setVisible(false);
        }
        else{  
            panels[index].setVisible(true);
        }
        ap.getParent().validate();  
    }  
   
    private int getPanelIndex(ActionPanel ap)  
    {  
        for(int j = 0; j < aps.length; j++)  
            if(ap == aps[j])  
                return j;  
        return -1;  
    }  
   
    private void assembleActionPanels()  
    {  
        String[] ids = { "Actions", "Control", "Macros"};  
        aps = new ActionPanel[ids.length];  
        for(int j = 0; j < aps.length; j++)  
            aps[j] = new ActionPanel(ids[j], this);  
    }  
   
    private void assemblePanels()  
    {  
        JPanel actPanel = new JPanel(new FlowLayout());  
        actPanel.setPreferredSize(new Dimension(200, 200));
        JPanel controlPanel = new JPanel(new FlowLayout());  
        controlPanel.setPreferredSize(new Dimension(200,200));
        JPanel macroPanel = new JPanel(new FlowLayout());
        macroPanel.setPreferredSize(new Dimension(200,200));
        
        Class[] classes = ClassFinder.findActions();
        
        for(int i = 0; i < classes.length; i++){
        	
        	Class act = classes[i];
        	String action = act.getName().substring(act.getName().lastIndexOf(".")+1);
        	String pkg = act.getPackage().getName().substring(act.getPackage().getName().lastIndexOf(".")+1);
        	
        	if(pkg.equals("Actions")){
        		CommandItem cmdIt = new CommandItem(action, "Actions");
        		if(action.equals("Chirp")){
        			cmdIt.addActionListener(new ChirpListener(new JFrame()));
        		}else if(action.equals("FunctionInvocation")){
        			cmdIt.addActionListener(new FunctionInvocationListener(new JFrame()));
        		}else if(action.equals("BeakColor")){
        			cmdIt.addActionListener(new BeakColorListener(new JFrame()));
        		}else if(action.equals("DropBeeper")){
        			cmdIt.addActionListener(new DropBeeperListener(new JFrame()));
        		}else if(action.equals("PickBeeper")){
        			cmdIt.addActionListener(new PickBeeperListener(new JFrame()));
        		}else{
        			cmdIt.addActionListener(cmdIt);
        		}
        		actPanel.add(cmdIt);
        	}else if(pkg.equals("Control")){
        		CommandItem cmdIt = new CommandItem(action, "Control");
        		
        		if(action.equals("If")){
        			cmdIt.addActionListener(new IfListener(new JFrame()));
        		}else if(action.equals("Repeat")){
        			cmdIt.addActionListener(new RepeatListener(new JFrame()));
        		}else if(action.equals("While")){
        			cmdIt.addActionListener(new WhileListener(new JFrame()));
        		}else if(action.equals("FunctionDefinition")){
        			cmdIt.addActionListener(new FunctionDefListener(new JFrame()));
        		}else if(action.equals("Comment")){
        			cmdIt.addActionListener(new CommentListener(new JFrame()));
        		}else if(action.equals("Include")){
        			cmdIt.addActionListener(new IncludeListener(new JFrame()));
        		}else{
        			cmdIt.addActionListener(cmdIt);
        		}
        		
        		controlPanel.add(cmdIt);
        	}
        	
        }
        
        
        panels = new JPanel[] { actPanel, controlPanel, macroPanel};
    }  
    
    private void addComponents(Component c1, Component c2, Container c,  
                               GridBagConstraints gbc)  
    {  
        gbc.anchor = gbc.EAST;  
        gbc.gridwidth = gbc.RELATIVE;  
        c.add(c1, gbc);  
        gbc.anchor = gbc.WEST;  
        gbc.gridwidth = gbc.REMAINDER;  
        c.add(c2, gbc);  
        gbc.anchor = gbc.CENTER;  
    }  
   
    JPanel getComponent()  
    {  
        JPanel panel = new JPanel(new FlowLayout()); 
        GridBagConstraints gbc = new GridBagConstraints();  
//        gbc.insets = new Insets(1,3,0,3);  
//        gbc.weightx = 1.0;  
        gbc.fill = gbc.HORIZONTAL;  
//        gbc.gridwidth = gbc.REMAINDER;  
        for(int j = 0; j < aps.length; j++)  
        {  
            panel.add(aps[j]);
            panel.add(panels[j]);
            panels[j].setVisible(false);  
        }  
        JLabel padding = new JLabel();  
//        gbc.weighty = 1.0;  
        panel.add(padding, gbc);  
        return panel;  
    }  
//   
//    public static void main(String[] args)  
//    {  
//        ExpandingPanels test = new ExpandingPanels();  
//        JFrame f = new JFrame();  
//        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
//        f.getContentPane().add(new JScrollPane(test.getComponent()));  
//        f.setSize(360,500);  
//        f.setLocation(200,100);  
//        f.setVisible(true);  
//    }  
}  